{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Training Step: 11  | total loss: \u001b[1m\u001b[32m2.83045\u001b[0m\u001b[0m | time: 15121.971s\n",
      "\u001b[2K\r",
      "| RMSProp | epoch: 001 | loss: 2.83045 - acc: 0.0235 -- iter: 0352/1360\n"
     ]
    }
   ],
   "source": [
    "\"\"\" Very Deep Convolutional Networks for Large-Scale Visual Recognition.\n",
    "Applying VGG 16-layers convolutional network to Oxford's 17 Category Flower\n",
    "Dataset classification task.\n",
    "References:\n",
    "    Very Deep Convolutional Networks for Large-Scale Image Recognition.\n",
    "    K. Simonyan, A. Zisserman. arXiv technical report, 2014.\n",
    "Links:\n",
    "    http://arxiv.org/pdf/1409.1556\n",
    "\"\"\"\n",
    "\n",
    "from __future__ import division, print_function, absolute_import\n",
    "\n",
    "import tflearn\n",
    "from tflearn.layers.core import input_data, dropout, fully_connected\n",
    "from tflearn.layers.conv import conv_2d, max_pool_2d\n",
    "from tflearn.layers.estimator import regression\n",
    "\n",
    "# Data loading and preprocessing\n",
    "import tflearn.datasets.oxflower17 as oxflower17\n",
    "X, Y = oxflower17.load_data(one_hot=True)\n",
    "\n",
    "# Building 'VGG Network'\n",
    "network = input_data(shape=[None, 224, 224, 3])\n",
    "\n",
    "network = conv_2d(network, 64, 3, activation='relu')\n",
    "network = conv_2d(network, 64, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2, strides=2)\n",
    "\n",
    "network = conv_2d(network, 128, 3, activation='relu')\n",
    "network = conv_2d(network, 128, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2, strides=2)\n",
    "\n",
    "network = conv_2d(network, 256, 3, activation='relu')\n",
    "network = conv_2d(network, 256, 3, activation='relu')\n",
    "network = conv_2d(network, 256, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2, strides=2)\n",
    "\n",
    "network = conv_2d(network, 512, 3, activation='relu')\n",
    "network = conv_2d(network, 512, 3, activation='relu')\n",
    "network = conv_2d(network, 512, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2, strides=2)\n",
    "\n",
    "network = conv_2d(network, 512, 3, activation='relu')\n",
    "network = conv_2d(network, 512, 3, activation='relu')\n",
    "network = conv_2d(network, 512, 3, activation='relu')\n",
    "network = max_pool_2d(network, 2, strides=2)\n",
    "\n",
    "network = fully_connected(network, 4096, activation='relu')\n",
    "network = dropout(network, 0.5)\n",
    "network = fully_connected(network, 4096, activation='relu')\n",
    "network = dropout(network, 0.5)\n",
    "network = fully_connected(network, 17, activation='softmax')\n",
    "\n",
    "network = regression(network, optimizer='rmsprop',\n",
    "                     loss='categorical_crossentropy',\n",
    "                     learning_rate=0.0001)\n",
    "\n",
    "# Training\n",
    "model = tflearn.DNN(network, checkpoint_path='model_vgg',\n",
    "                    max_checkpoints=1, tensorboard_verbose=0)\n",
    "model.fit(X, Y, n_epoch=500, shuffle=True,\n",
    "          show_metric=True, batch_size=32, snapshot_step=500,\n",
    "          snapshot_epoch=False, run_id='vgg_oxflowers17')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
